<!DOCTYPE html>
<html>

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>规格管理</title>
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="../css/style.css">
	<script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="../plugins/bootstrap/js/bootstrap.min.js"></script>
    
</head>

<body class="hold-transition skin-red sidebar-mini" >
  <!-- .box-body -->                
                    <div class="box-header with-border">
                        <h3 class="box-title">规格管理</h3>
                    </div>
                    <div class="box-body">
                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#editModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" title="删除" onclick="deleteSpec()" ><i class="fa fa-trash-o"></i> 删除</button>
                                        
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                                    </div>
                                </div>
                            </div>
                            <div class="box-tools pull-right">
                                <div class="has-feedback">
							                    规格名称：<input type="text" id="specNamess" >
									<button class="btn btn-default" onclick="getData()" >查询</button>
                                </div>
                            </div>
                            <!--工具栏/-->
							<!--数据列表-->
<!--							<table id="dataList" class="table table-bordered table-striped table-hover dataTable">-->
								<div id="specification"></div>
							<!--数据列表/-->
						</div>
                        <!-- 数据表格 /--> 
                     </div>
                    <!-- /.box-body -->                    
	          		    
                                
<!-- 编辑窗口 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">增加规格</h3>
		</div>
		<div class="modal-body">							
			
			<table  class="table table-bordered table-striped"  width="800px">
		      	<tr>
		      		<td>规格名称</td>
		      		<td><input  class="form-control" id="specName" placeholder="规格名称" >  </td>
		      	</tr>
			 </table>				
			 
			 <!-- 规格选项 -->
			 <div class="btn-group">
                  <button type="button" class="btn btn-default" title="新建" ><i class="fa fa-file-o"></i> 新增规格选项</button>
                  
             </div>
			 
			 <table id="specTable" class="table table-bordered table-striped table-hover dataTable">
                    <thead>
                        <tr>
						  <th class="sorting">规格选项</th>
						  <th class="sorting">排序</th>
						  <th class="sorting">操作</th>
						</tr>
                    </thead>
                    <tbody>
                      <tr>
                           
				            <td>
				            	<input  class="form-control" placeholder="规格选项"> 
				            </td>
				            <td>
				            	<input  class="form-control" placeholder="排序"> 
				            </td>
							<td>
								<button type="button" class="btn btn-default" title="删除" ><i class="fa fa-trash-o"></i> 删除</button>
							</td>
                      </tr>
					  <tr>
                         
				            <td>
				            	<input  class="form-control" placeholder="规格选项"> 
				            </td>
				            <td>
				            	<input  class="form-control" placeholder="排序"> 
				            </td>
							<td>
								<button type="button" class="btn btn-default" title="删除" ><i class="fa fa-trash-o"></i> 删除</button>
							</td>
                      </tr>
					  <tr>
                           
				            <td>
				            	<input  class="form-control" placeholder="规格选项"> 
				            </td>
				            <td>
				            	<input  class="form-control" placeholder="排序"> 
				            </td>
							<td>
								<button type="button" class="btn btn-default" title="删除" ><i class="fa fa-trash-o"></i> 删除</button>
							</td>
                      </tr>
                    </tbody>
			  </table> 
			
			
		</div>
		<div class="modal-footer">						
			<button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="toAdd()">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>


  <!-- 修改窗口 -->
<div class="modal fade" id="updateModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabels">增加规格</h3>
		</div>
		<div class="modal-body">

			<table  class="table table-bordered table-striped"  width="800px">
				<!--  -->
				<div id="trs"></div>
			 </table>

			 <!-- 规格选项 -->
			 <div class="btn-group">
                  <button type="button" class="btn btn-default" title="新建" ><i class="fa fa-file-o"></i> 修改规格选项</button>

             </div>

			<div id="opTable"></div>


		</div>
		<div class="modal-footer">
			<button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="toUpdate()">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>

</body>

<script>
	$(function (){
		getData();
	});
	//查询所有信息
	function getData(){
		$.ajax({
			url:"/spec/getSpecInfo",
			type:"post",
			dataType:"json",
			async:false,
			data : {specName:$("#specNamess").val()},
			success:function (result){
				if (result.code==10000){
					var list = result.result;
					var table="";
					table +='<table id=\"dataList\" class=\"table table-bordered table-striped table-hover dataTable\">';
					table +='<thead>';
					table +='<tr>';
					table +='<th className="" style="padding-right:0px">';
					table +='<input id="selall" type="checkbox" className="icheckbox_square-blue">';
					table +='</th>';
					table +='<th className="sorting_asc">规格ID</th>';
					table +='<th className="sorting">规格名称</th>';
					table +='<th className="text-center">操作</th>';
					table +='</tr>';
					table +='</thead>';
					table +='<tbody>';
					for (var i = 0; i < list.length; i++) {
						table +='<tr>';
						table +='<td><input type="checkbox" name="check" value="'+list[i].id+'"></td>';
						table +='<td>'+list[i].id+'</td>';
						table +='<td>'+list[i].specName+'</td>';
						table+='<td className="text-center">';
						table+='<button type="button" className="btn bg-olive btn-xs" data-toggle="modal" onclick="updateSpec('+list[i].id+')" data-target="#updateModal">修改</button>';
						table+='</td>';
						table+='</tr>';
					}
					table +='</tbody>';
					table +="</table>";
				}
				$("#specification").html(table);

			},
			error : function (result){
				alert("网络异常,请重新刷新")
			}
		})
	}

	/*  增加  */
	function toAdd() {
		// 先把前端的对象转换成 字符串   然后后端再进行处理
		var optionList = [];
		var trs = $("#specTable").find("tr");
		for (var i = 1; i < trs.length ; i++) {
			var tds = $(trs[i]).find("td");
			// {} 在js 中表示对象 json 对象 map
			var option = {id:null,optionName:$(tds[0]).find("input").val(),orders:$(tds[1]).find("input").val(),specId:null}
			optionList.push(option);
		}
		var optionListJson = JSON.stringify(optionList);

		$.ajax({
			url : "/spec/insertSpecInfo",
			type : "post",
			dataType : "json",
			async : true,
			data : {specName:$("#specName").val(),optionListJson:optionListJson},
			success : function (result) {
				if (result.code == 10000){
					window.location.reload();
					alert(result.message);
				}else if (result.code == 10001){
					alert(result.message)
				}
			},
			error : function (result) {
				alert("网络错误,增加失败");
			}
		})
	}

	/*  回显  */
	function updateSpec(id) {
		$.ajax({
			url : "/spec/updateSpec/"+id,
			type : "post",
			dataType : "json",
			async : true,
			success : function (result) {
				var specName = result.tbSpecification.specName;
				var iddd = result.tbSpecification.id;
				var tr = '<tr>';
					tr += '<td>规格名称</td>';
					tr += '<input type="hidden" value="'+ iddd +'" id="ddd">';
					tr += '<td><input className="form-control" id="specNames" value="'+ specName +'" placeholder="规格名称"></td>';
					tr += '</tr>';
				$("#trs").html(tr);

				var soPn = result.list;
				var tableddd = '<table id="opTables" className="table table-bordered table-striped table-hover dataTable">';

					tableddd += '<thead><tr><th className="sorting">规格选项</th>';
					tableddd += '<th className="sorting">排序</th>';
					tableddd += '<th className="sorting">操作</th></tr></thead>';
					tableddd += '<thead>';
				for (var i = 0; i < soPn.length; i++) {
					tableddd += '<tr><td><input className="form-control" value="'+ soPn[i].optionName +'" placeholder="规格选项"></td>';
					tableddd += '<td><input className="form-control" value="'+ soPn[i].orders +'"  placeholder="排序"></td>';
					tableddd += '<td><button type="button" className="btn btn-default" title="删除"><i className="fa fa-trash-o"></i> 删除</button></td></tr>';
				}
					tableddd += '</thead>';
					tableddd += '</table>';
				$("#opTable").html(tableddd);
			},
			error : function (result) {
				alert("网络错误,增加失败");
			}
		})
	}
	/*  修改  */
	function toUpdate() {
		var optionList = [];
		var trs = $("#opTables").find("tr");
		for (var i = 1; i < trs.length ; i++) {
			var tds = $(trs[i]).find("td");
			// {} 在js 中表示对象 json 对象 map
			var option = {id:null,optionName:$(tds[0]).find("input").val(),orders:$(tds[1]).find("input").val(),specId:null}
			optionList.push(option);
		}
		var optionListJson = JSON.stringify(optionList);
		$.ajax({
			url : "/spec/updateSpecInfo",
			type : "post",
			dataType : "json",
			async : true,
			data : {id:$("#ddd").val(),specName:$("#specNames").val(),optionListJson:optionListJson},
			success : function (result) {
				if (result.code == 10000){
					window.location.reload();
					alert(result.message);
				}else if (result.code == 10001){
					alert(result.message)
				}
			},
			error : function (result) {
				alert("网络错误,修改失败");
			}
		})
	}

	/*  删除  */
	function deleteSpec() {
		var ids = [];
		var checks = $("[name=check]");
		for (var i = 0; i <checks.length; i++) {
			if (checks[i].checked){
				ids.push(checks[i].value);
			}
		}
		console.log(ids);


		$.ajax({
			url : "/spec/deleteSpecInfo?ids="+ids,
			type : "post",
			dataType : "json",
			async : true,
			success : function (result) {
				if (result.code == 10000){
					window.location.reload();
					alert(result.message);
				}else if (result.code == 10001){
					alert(result.message)
				}
			},
			error : function (result) {
				alert("网络错误,删除失败");
			}
		})
	}
</script>

</html>